#!/bin/bash

app_dir=./app

if [ ! -d $app_dir ]; then
    echo "error, not have app directory"
    exit 1
fi

src_so=$1
lib_dir=$2

echo "Creating app to check $src_so, lib_dir: $lib_dir"

# needed=$(objdump -x $src_so | grep NEEDED | awk '{print $2}')

# echo "$(basename $src_so) needs: $(echo $needed)"

# export so_dir=$(realpath $(dirname $src_so))
export lib_dir=$lib_dir
# 将needed转换为空格分隔，并且去掉.so的后缀
# export libs=$(echo $(echo "$(basename $src_so) $needed" | tr ' ' '\n' | sed -e 's/\.[^ ]*//g' | sed -e 's/\blib//g'))
export libs=$(realpath $src_so)

# echo "so_dir: $so_dir"

cd $app_dir

rm -rf ./output
mkdir output

cd output
cmake ../ -DCMAKE_C_COMPILER=$compiler >> /dev/null
make 2>&1 | tee make.log >> /dev/null

# 提取未定义符号信息，以行的形式打印
while read line; do
    if [[ $line == *"undefined"* ]]; then
        symble=$(echo $line | awk '{print $5}')
        echo "$(basename $src_so),$symble" >> ../../symble_deps.csv
    fi
done < make.log

# 提取依赖so找不到信息，以行的形式打印
while read line; do
    if [[ $line == *"not found"* ]]; then
        so=$(echo $line | awk '{print $3}')
        echo "$(basename $src_so),$so"  >> ../../so_deps.csv
    fi
done < make.log

cd ../

rm -rf ./output